package com.yjha.lzmarathonsys.service.impl;

import com.yjha.lzmarathonsys.bean.TbSpecialClockin;
import com.yjha.lzmarathonsys.dto.Param;
import com.yjha.lzmarathonsys.service.TbSpecialClockinService;
import com.yjha.lzmarathonsys.utils.R;
import com.yjha.lzmarathonsys.vo.TbSpecialVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjha.lzmarathonsys.utils.PageUtils;
import com.yjha.lzmarathonsys.utils.Query;

import com.yjha.lzmarathonsys.mapper.TbSpecialMapper;
import com.yjha.lzmarathonsys.bean.TbSpecial;
import com.yjha.lzmarathonsys.service.TbSpecialService;


@Service("tbSpecialService")
public class TbSpecialServiceImpl extends ServiceImpl<TbSpecialMapper, TbSpecial> implements TbSpecialService {

    @Autowired
    private TbSpecialClockinService  tbSpecialClockinService;

    @Override
    public PageUtils queryPage(Param params) {
        Map<String,Object> map = new HashMap<>();
        String page1 = params.getPage();
        String limit1 = params.getLimit();
        String key = params.getKey();

        map.put("page",page1);
        map.put("limit",limit1);
        map.put("key",key);

        QueryWrapper<TbSpecial> tbSpecialQueryWrapper = new QueryWrapper<>();
        if (key != null && key != "" ) {
            tbSpecialQueryWrapper.like("tb_specialname",key);
        }
        IPage<TbSpecial> page = this.page(
                new Query<TbSpecial>().getPage(map),
               tbSpecialQueryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        List<TbSpecialVo> collect = page.getRecords().stream().map(tbSpecial -> {
            TbSpecialVo tbSpecialVo = new TbSpecialVo();
            QueryWrapper<TbSpecialClockin> specialClockinQueryWrapper = new QueryWrapper<TbSpecialClockin>().eq("tb_specialid", tbSpecial.getTbId());
            BeanUtils.copyProperties(tbSpecial, tbSpecialVo);
            tbSpecialVo.setTbPunchnum(tbSpecialClockinService.list(specialClockinQueryWrapper).size());
            QueryWrapper<TbSpecialClockin> and = new QueryWrapper<TbSpecialClockin>().eq("tb_specialid", tbSpecial.getTbId()).and(w -> w.eq("tb_status", 2));
            tbSpecialVo.setTbUnverifiednum(tbSpecialClockinService.list(and).size());
            return tbSpecialVo;
        }).sorted((o1, o2) -> o2.getTbUnverifiednum().compareTo(o1.getTbUnverifiednum())).collect(Collectors.toList());
        pageUtils.setList(collect);
        return pageUtils;
    }

    @Override
    public R listSpecialAll() {
        // QueryWrapper<TbSpecial> tbSpecialQueryWrapper = new QueryWrapper<>();
        List<TbSpecial> list = this.list();
        return R.ok().put("data",list);
    }

    @Override
    public R saveTbSpecial(TbSpecial tbSpecial) {
        QueryWrapper<TbSpecial> tbSpecialname = new QueryWrapper<TbSpecial>().eq("tb_specialname", tbSpecial.getTbSpecialname());
        if (this.getOne(tbSpecialname) != null) {
            return R.error(9999,"专题名称已存在");
        }
        this.save(tbSpecial);
        return R.ok();
    }

    @Override
    public R updateByIdTbSpecial(TbSpecial tbSpecial) {
        QueryWrapper<TbSpecialClockin> specialClockinQueryWrapper = new QueryWrapper<TbSpecialClockin>().eq("tb_specialid", tbSpecial.getTbId());
        List<TbSpecialClockin> list = tbSpecialClockinService.list(specialClockinQueryWrapper);
        if (list.size() > 0) {
            return R.error(400,"专题在使用中，不可进行修改");
        }
        this.updateById(tbSpecial);
        return R.ok();
    }

    @Override
    public R removeByIdsDelete(List<Integer> list) {
        for (Integer item : list) {
            QueryWrapper<TbSpecialClockin> specialClockinQueryWrapper = new QueryWrapper<TbSpecialClockin>().eq("tb_specialid", item);
            List<TbSpecialClockin> list1 = tbSpecialClockinService.list(specialClockinQueryWrapper);
            QueryWrapper<TbSpecial> tbSpecialQueryWrapper = new QueryWrapper<TbSpecial>().eq("tb_id", item);
            tbSpecialQueryWrapper.eq("tb_id",item);
            TbSpecial tbSpecial = this.getOne(tbSpecialQueryWrapper);
            if (list1.size() > 0) {
                return R.error(400,"专题 " + tbSpecial.getTbSpecialname() + " 在使用中，不可进行删除");
            }
            this.removeById(item);
        }
        return R.ok();
    }

}